<?php

namespace App\Admin\Controllers\Audit;

use App\Admin\Actions\Audit\AuditAuctionAction;
use App\Admin\Grid\NGrid;
use App\Models\AuctionModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Illuminate\Support\Facades\DB;

class AuditAuctionController extends AdminController
{
    protected $title = '拍卖审核';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new AuctionModel);
        $grid->model()->where('status', 1)->where('audit_status', 0)->orderByDesc('id');
        $grid->expandFilter();
        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('serial_number', __('编号'));
                $filter->equal('status', __('状态'))->select([0 => '下架', 1 => '上架']);
                $filter->equal('audit_status', __('审核状态'))->select([0 => '待审核', 1 => '审核成功', 2 => '审核失败']);
            });
        });
        $grid->column('id', 'ID')->sortable();
        $list = DB::table('shop_category')->pluck('category_name', 'category_id')->toArray();
        $grid->column('category_id_one', '商品分类')->display(function ($field) use ($list) {

            return implode('-', [$list[$field], $list[$this->category_id_two], $list[$this->category_id_three]]);
        });
        $grid->column('brand', '品牌');
        $grid->column('title', '标题');
        $grid->column('description', '商品详情')->display(function ($field) {
            if (strlen($this->description) > 10) return mb_substr($this->description, 0, 10, "utf-8") . "...";
            else return $this->description;
        });
        $grid->column('file_id', '商品图片')->display(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 60, 60);
        $grid->column('sort', '排序')->sortable();
        $grid->column('recommend', '推荐')->using([0 => '否', 1 => '是']);
        $grid->column('status', '状态')->using([0 => '下架', 1 => '上架']);
        $grid->column('sell_out', '在售状态')->using([0 => '未售完', 1 => '已经售完']);
        $grid->column('expired', '过期状态')->using([0 => '否', 1 => '是']);
        $grid->column('audit_status', '审核状态')->using([0 => '待审核', 1 => '审核成功', '2' => '审核失败']);
        $grid->column('is_protect', '是否保护动物')->using([0 => '否', 1 => '是']);
        $grid->column('is_feral', '动物类型')->using([0 => '家养动物', 1 => '野生']);
        $grid->column('stage', '阶段')->using([0 => '苗子', 1 => '亚成', 2 => '成体']);
        $grid->column('gene', '基因');
        $grid->column('capping_price', '一口价');
        $grid->column('tag', __('标签'))->display(function ($parameter) {
            return view('auctionTags', ['id' => $this->id, 'json' => $parameter]);
        });
        $grid->column('eat', '喂养饲料');
        $grid->column('guide', '指南');
        $grid->column('start_price', '起拍价');
        $grid->column('bail_price', '保证金');
        $grid->column('start_end_time', '起拍时间范围')->display(function () {
            return $this->start_at . ' 至 ' . $this->end_at;
        });

        $grid->column('source_number', '溯源编码');
        $grid->column('inc', '加价幅度');
        $grid->column('freight', '运费');
        $grid->column('publisher.user_name', '发布人');
        $grid->column('remark', '备注');
        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableDelete();
            $action->disableView();
            $action->add(new AuditAuctionAction);
        });
        $grid->disableActions(false);
        return $grid;
    }
}
